import { defineStore } from 'pinia';

export const useUserStore = defineStore('user', {
  state: () => ({
    users: [
      { id: 1, username: 'admin', password: 'admin123', email: 'admin@example.com', role: '管理员', active: true },
      { id: 2, username: 'user1', password: 'user123', email: 'user1@example.com', role: '普通用户', active: false },
      { id: 3, username: 'user2', password: 'user234', email: 'user2@example.com', role: '普通用户', active: true }
    ]
  }),
  actions: {
    addUser(user) {
      const maxId = this.users.reduce((max, u) => u.id > max ? u.id : max, 0);
      user.id = maxId + 1;
      this.users.push(user);
    },
    updateUser(updatedUser) {
      const index = this.users.findIndex(user => user.id === updatedUser.id);
      if (index !== -1) {
        this.users[index] = updatedUser;
      }
    },
    deleteUser(id) {
      this.users = this.users.filter(user => user.id !== id);
    },
    toggleUserStatus(id) {
      const user = this.users.find(user => user.id === id);
      if (user) {
        user.active = !user.active;
      }
    }
  }
});
